#include <vector>
#include <utility>
#include <stack>
using namespace std;
class Solution
{
public:
    vector<int> dailyTemperatures(vector<int> &temperatures)
    {
        vector<int> res(temperatures.size(), 0);
        if (temperatures.size() < 2)
            return res;
        //栈保存气温和列表下标 
        stack<pair<int, int>> st;
        for (int i = 0; i < temperatures.size(); i++)
        {
            while (!st.empty())
            {
                auto pr = st.top();
                //更新答案
                if (temperatures[i] > pr.first)
                {
                    res[pr.second] = i - pr.second;
                    st.pop();
                }
                else
                    break;
            }
            //入栈
            st.push({temperatures[i], i});
        }
        return res;
    }
};